﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Collections;
using System.Security;
using agsXMPP.protocol.client;
using agsXMPP;

namespace PoshXmpp
{
    [Cmdlet(VerbsCommon.Add, "Contact", SupportsShouldProcess = true)]
    public class AddContactCommand : XmppCommandBase
    {
        #region Parameters

        [Parameter( ParameterSetName="ByName", Position=0,
            ValueFromPipelineByPropertyName=true,
        HelpMessage = "The JabberId to add as a contact")]
        [Alias("Base")]
        public string JabberId { get; set; }

        [Parameter(
        Mandatory = false,
        HelpMessage = "Show all available messages")]
        public SwitchParameter All { get; set; }
        #endregion

        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            if (ParameterSetName == "ByName")
            {
                Client.AddBuddy(new Jid(JabberId));
            }

            foreach (Jid m in Client.ContactRequests)
            {
                if (ShouldProcess(m.Bare, "Allow them to see your presence information"))
                {
                    Client.AddBuddy(m);
                }

                // if not "All" then we only process one message
                if (!All.ToBool()) { break; }
            }
        }
    }
}
